# -*- coding: utf-8 -*-
import sys
import logging
import sqlalchemy
from lxml import etree
from xadmin.model import *
from xadmin.lib.base import *
from xadmin.lib.associationfactory import AssociationFactory

log = logging.getLogger(__name__)

class AssociationsController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('association', 'associations')


    def index(self, format='html'):
        """GET /associations: All items in the collection."""
        # url_for('associations')
        print 'hola'
        return

    def listByType(self, project, type):
        """GET /associations: All items in the collection."""
        assoc = AssociationFactory().getAssociation(type)
        response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        result = assoc.getByProject(project)
        if result:
            return result
        else:
            abort(404)

    def create(self, type):
        """POST /associations: Create a new item."""
        # url_for('associations')
        del response.headers['Content-Type']
        response.headers['Location'] = url_for('createassociation', type=type)
        return

    def createByType(self, project, type):
        assoc = AssociationFactory().getAssociation(type)
        content = request.body.read(int(request.environ['CONTENT_LENGTH']))
        if not content:
            content = request.params.get('xml')
        result = assoc.create(project, content)
        if result:
            del response.headers['Content-Type']
            response.headers['Location'] = '/associations/%s/%s' % (type, result.id)
            response.status_code = 201
            return
        else:
            abort(400)

    def new(self, format='html'):
        """GET /associations/new: Form to create a new item."""
        # url_for('new_association')
        pass

    def update(self, id):
        """PUT /associations/id: Update an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(h.url_for('association', id=ID),
        #           method='put')
        # url_for('association', id=ID)
        pass

    def delete(self, id):
        """DELETE /associations/id: Delete an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="DELETE" />
        # Or using helpers:
        #    h.form(h.url_for('association', id=ID),
        #           method='delete')
        # url_for('association', id=ID)
        pass

    def show(self, id, format='html'):
        """GET /associations/id: Show a specific item."""
        # url_for('association', id=ID)
        print 'hola'
        return

    def edit(self, id, format='html'):
        """GET /associations/id;edit: Form to edit an existing item."""
        # url_for('edit_association', id=ID)
        pass
